//
//  ClassifyDetailsViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassifyDetailsViewController.h"
#import "ClassifyDetailsCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "ClassifyDetailsModel.h"

@interface ClassifyDetailsViewController ()
@property(nonatomic, retain)UITableView *popularityTableView;
@property(nonatomic, retain)UITableView *recentTableView;
@property(nonatomic, retain)NSMutableArray *popularityArr;//存放人气model的数组
@property(nonatomic, retain)NSMutableArray *recentArr;//存放最近model的数组
@property(nonatomic, retain)ClassifyDetailsModel *classifyDetails;
@property(nonatomic, assign)NSInteger pageIndex;
@end

@implementation ClassifyDetailsViewController

#pragma mark 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.popularityArr = [NSMutableArray array];
        self.recentArr = [NSMutableArray array];
        self.pageIndex = 0;
    }
    return self;
}

#pragma mark 释放内存
-(void)dealloc
{
    [_popularityTableView release];
    [_recentTableView release];
    [_popularityArr release];
    [_recentArr release];
    [_classifyDetails release];
    [super dealloc];
}

#pragma mark viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.tempTitle;
    [self popularityData];
    [self recentData];
    [self createClassifyDetailsView];
    [self method];
    [self addAndRefresh];
}

#pragma mark 创建视图
- (void)createClassifyDetailsView
{
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"人气火爆", @"最近更新"]];
    seg.frame = CGRectMake(50, 69, 275, 35);
    [seg addTarget:self action:@selector(choose:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
    [seg release];
    //**********
    self.popularityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 109, 375, 517) style:UITableViewStylePlain];
    self.popularityTableView.rowHeight = 150;
    [self.view addSubview:self.popularityTableView];
    [_popularityTableView release];
    self.popularityTableView.dataSource = self;
    self.popularityTableView.delegate = self;
    //***********
    self.recentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 109, 375, 517) style:UITableViewStylePlain];
    self.recentTableView.rowHeight = 150;
    [self.view addSubview:self.recentTableView];
    [_recentTableView release];
    self.recentTableView.dataSource = self;
    self.recentTableView.delegate = self;
    [self.view bringSubviewToFront:self.popularityTableView];
}

#pragma mark segment点击方法
- (void)choose:(id)sander
{
    UISegmentedControl *seg = (UISegmentedControl *)sander;
    NSInteger index = seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            NSLog(@"人气火爆");
            [self.view bringSubviewToFront:self.popularityTableView];
            break;
        case 1:
            NSLog(@"最近更新");
            [self.view bringSubviewToFront:self.recentTableView];
            break;
        default:
            break;
    }
}

#pragma mark TableView两种代理必须实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if ([tableView isEqual:self.popularityTableView]) {
        count = self.popularityArr.count;
    }else if ([tableView isEqual:self.recentTableView]){
        count = self.recentArr.count;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ClassifyDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[ClassifyDetailsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    if ([tableView isEqual:self.popularityTableView] && self.popularityArr.count != 0) {
        self.classifyDetails = self.popularityArr[indexPath.row];
    }else if ([tableView isEqual:self.recentTableView] && self.recentArr.count != 0){
        self.classifyDetails = self.recentArr[indexPath.row];
    }
    cell.classifyDetBookNameLabel.text = self.classifyDetails.BookName;
    cell.classifyDetBookDescriptionLabel.text = self.classifyDetails.BookDescription;
    NSURL *url = [NSURL URLWithString:self.classifyDetails.BookIconOtherURL];
    [cell.classifyDetBookIconOtherURLImageView sd_setImageWithURL:url];
    return cell;
}

#pragma mark 人气火爆数据解析
- (void)popularityData
{
    
    NSString *url = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=booklist&pageSize=20&cataID=%ld&state=0&order=0&pageIndex=%ld", self.tempID, self.pageIndex];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [NSMutableArray array];
        array = dic[@"Booklist"];
        for (int i = 0; i < array.count; i++) {
            self.classifyDetails = [[ClassifyDetailsModel alloc] init];
            [self.classifyDetails setValuesForKeysWithDictionary:array[i]];
            [self.popularityArr addObject:self.classifyDetails];
        }
        [self.popularityTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
        NSLog(@"%@", error);
    }];
}

#pragma mark 最近更新数据解析
- (void)recentData
{
    self.pageIndex++;
    NSString *url = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=booklist&pageSize=20&cataID=%ld&state=0&order=1&pageIndex=%ld", self.tempID, self.pageIndex];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [NSMutableArray array];
        array = dic[@"Booklist"];
        for (int i = 0; i < array.count; i++) {
            self.classifyDetails = [[ClassifyDetailsModel alloc] init];
            [self.classifyDetails setValuesForKeysWithDictionary:array[i]];
            [self.recentArr addObject:self.classifyDetails];
        }
        [self.recentTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
        NSLog(@"%@", error);
    }];
}

#pragma mark 下拉刷新上拉加载
- (void)method
{
    [self.popularityTableView addLegendHeaderWithRefreshingBlock:^{
        [self.popularityArr removeAllObjects];
        self.pageIndex = 0;
        [self popularityData];
        [self.popularityTableView reloadData];
        [self.popularityTableView.header endRefreshing];
    }];
    [self.popularityTableView.legendHeader beginRefreshing];
    [self.popularityTableView addLegendFooterWithRefreshingBlock:^{
        self.pageIndex++;
        [self popularityData];
        [self.popularityTableView reloadData];
        [self.popularityTableView.footer endRefreshing];
    }];
}

- (void)addAndRefresh
{
    [self.recentTableView addLegendHeaderWithRefreshingBlock:^{
        [self.popularityArr removeAllObjects];
        self.pageIndex = 0;
        [self recentData];
        [self.recentTableView reloadData];
        [self.recentTableView.header endRefreshing];
    }];
    [self.recentTableView.legendHeader beginRefreshing];
    [self.recentTableView addLegendFooterWithRefreshingBlock:^{
        self.pageIndex++;
        [self recentData];
        [self.recentTableView reloadData];
        [self.recentTableView.footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
